package cn.andev.app.morsesignal;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class Config extends Activity {
	private ImageView iconRing;
	private CheckBox autoRing;
	private Spinner spBkgColor;
	
	private Button btnApply;
	private Button btnCancel;
	//private Button btnReset;

	private static String KEY_RING = "ring";
	private static String KEY_COLOR = "color";
	
	public static String WHITE = "White";
	public static String RED = "Red";
	public static String YELLOW = "Yellow";
	public static String GREEN = "Green";
	public static String BLUE = "Blue";
	public static String ORANGE = "Orange";
	
	// Default settings
	public static boolean bRing = false;
    public static int color = Color.WHITE;
    public static final int orange = Color.rgb(0xEE, 0x77, 0x00);
    
    //private boolean flag = true;
    
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.config);
		popupObject();
		readSavedConfig();
		renderUI();
		attachEvent();
		
		MorseSignal.mCurrentMode = MorseSignal.MODE_CONFIG;
	}

	private void readSavedConfig(){
		MorseSignal.db.openDB();
		
		if(MorseSignal.db.readConfig(KEY_RING) == "yes")
			bRing = true;
		else
			bRing = false;
		
		String colorStr = MorseSignal.db.readConfig(KEY_COLOR);
		if(colorStr == WHITE)
			color = Color.WHITE;
		else if(colorStr == RED)
			color = Color.RED;
		else if(colorStr == BLUE)
			color = Color.BLUE;
		else if(colorStr == GREEN)
			color = Color.GREEN;
		else if(colorStr == YELLOW)
			color = Color.YELLOW;
		else if(colorStr == ORANGE)
			color = this.orange;
		else 
			color = Color.WHITE;
		
		MorseSignal.db.close();
	}
	
	private void renderUI() {
		// Color Spinner
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(Config.this,
				R.array.color_array, android.R.layout.simple_spinner_item);
		
		  int position = 0;
          switch (color){
          	case Color.RED:
          		adapter = ArrayAdapter.createFromResource(Config.this,
              	      R.array.color_array, R.layout.redtext);
          		position = 1;
          	case Color.GREEN:
          		adapter = ArrayAdapter.createFromResource(Config.this,
                	      R.array.color_array, R.layout.redtext);
          		position = 2;
          	case Color.BLUE:
          		adapter = ArrayAdapter.createFromResource(Config.this,
              	      R.array.color_array, R.layout.redtext);
          		position = 3;
          	case Color.YELLOW:
          		adapter = ArrayAdapter.createFromResource(Config.this,
              	      R.array.color_array, R.layout.redtext);
          		position = 4;
          	case Color.WHITE:
          	default:
          		if(color == this.orange){
          			adapter = ArrayAdapter.createFromResource(Config.this,
                    	      R.array.color_array, R.layout.redtext);
          			position = 5;
          		}
          		break;
          }
          
          // Set text color in the spinner
          adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          spBkgColor.setAdapter(adapter);
          
          // Set saved ring & color
          autoRing.setChecked(bRing);
          spBkgColor.setSelection(position);
          spBkgColor.setSelection(Spinner.INVALID_POSITION);
	}
	
	private void popupObject() {
		iconRing = (ImageView) findViewById(R.id.iconRing);
		autoRing = (CheckBox) findViewById(R.id.autoRing);
		spBkgColor = (Spinner) findViewById(R.id.spinnerBkgColor);
		
		btnApply = (Button) findViewById(R.id.btnSettingApply);
		btnCancel = (Button) findViewById(R.id.btnSettingCancel);
		//btnReset = (Button) findViewById(R.id.btnSettingReset);
	}

	private void attachEvent() {
		autoRing.setOnCheckedChangeListener(ringChangedListener);
		
		btnApply.setOnClickListener(applyListener);
		btnCancel.setOnClickListener(cancelListener);
		//btnReset.setOnClickListener(resetListener);
		
		spBkgColor.setOnItemSelectedListener(new OnItemSelectedListener(){ 
            @Override 
            public void onItemSelected(AdapterView parent, View v, int position, long id) {
            	  //int position = spBkgColor.getSelectedItemPosition();
            	  //Spinner sp = (Spinner) parent;
            	  //String item = sp.getSelectedItem().toString();
            	  //ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(Config.this,
          		  //	R.array.color_array, android.R.layout.simple_spinner_item);
            	  
            	  /*if(item == WHITE){
                		color = Color.WHITE;
            	  }
            	  else if(item == RED){
                		color = Color.RED;
                		adapter = ArrayAdapter.createFromResource(Config.this,
                    	      R.array.color_array, R.layout.redtext);
            	  }
            	  else if(item == GREEN){
                		color = Color.GREEN;
                		adapter = ArrayAdapter.createFromResource(Config.this,
                      	      R.array.color_array, R.layout.redtext);
            	  }
            	  else if(item == BLUE){
                		color = Color.BLUE;
                		adapter = ArrayAdapter.createFromResource(Config.this,
                      	      R.array.color_array, R.layout.redtext);
            	  }
            	  else if(item == YELLOW){
                		color = Color.YELLOW;
                		adapter = ArrayAdapter.createFromResource(Config.this,
                      	      R.array.color_array, R.layout.redtext);
            	  }
            	  if(item == ORANGE){
                		color = Color.rgb(0xEE, 0x77, 0x00);//Orange
                		adapter = ArrayAdapter.createFromResource(Config.this,
                      	      R.array.color_array, R.layout.redtext);
            	  }
            	  else {
                		color = Color.WHITE;
            	  }*/
               
                  switch (position){
                  	case 0:
                  		color = Color.WHITE;
                  		break;
                  	case 1:
                  		color = Color.RED;
                  		/*adapter = ArrayAdapter.createFromResource(Config.this,
                      	      R.array.color_array, R.layout.redtext);*/
                  		break;
                  	case 2:
                  		color = Color.GREEN;
                  		/*adapter = ArrayAdapter.createFromResource(Config.this,
                        	      R.array.color_array, R.layout.redtext);*/
                  		break;
                  	case 3:
                  		color = Color.BLUE;
                  		/*adapter = ArrayAdapter.createFromResource(Config.this,
                        	      R.array.color_array, R.layout.redtext);*/
                  		break;
                  	case 4:
                  		color = Color.YELLOW;
                  		/*adapter = ArrayAdapter.createFromResource(Config.this,
                        	      R.array.color_array, R.layout.redtext);*/
                  		break;
                  	case 5:
                  		color = Color.rgb(0xEE, 0x77, 0x00);//Orange
                  		/*adapter = ArrayAdapter.createFromResource(Config.this,
                        	      R.array.color_array, R.layout.redtext);*/
                  		break;
                  	default:
                  		color = Color.WHITE;
                  		break;
                  }
                  
                  // Set text color in the spinner
                  //adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                  //spBkgColor.setAdapter(adapter);
                  //spBkgColor.setSelection(position);
                  spBkgColor.setBackgroundColor(color);
            } 

            @Override 
            public void onNothingSelected(AdapterView arg0) { 
                  
            }			
        });
	}

	private CompoundButton.OnCheckedChangeListener ringChangedListener = new CompoundButton.OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton buttonView,	boolean isChecked) {
			if(isChecked){
				iconRing.setImageResource(R.drawable.ring);
				
				bRing = true;
				MorseSignal.db.openDB();
				MorseSignal.db.saveConfig(KEY_RING, "yes");
				MorseSignal.db.close();
				
			} else {
				iconRing.setImageResource(R.drawable.ring_mute);
				
				bRing = false;
				MorseSignal.db.openDB();
				MorseSignal.db.saveConfig(KEY_RING, "no");
				MorseSignal.db.close();
			}
		}
	};
	
	private OnClickListener applyListener = new OnClickListener() {
		public void onClick(View v) {
			Toast.makeText(Config.this, "setting saved", Toast.LENGTH_SHORT).show();
		}
	};

	private OnClickListener cancelListener = new OnClickListener() {
		public void onClick(View v) {
			// TODO: cancel button pressed
		}
	};

	/*private OnClickListener resetListener = new OnClickListener() {
		public void onClick(View v) {

		}
	};*/

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

        //menu.add(0, MENU_ITEM_ABOUT, 0, R.string.menu_about)
		//	.setIcon(R.drawable.about);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		/*switch (item.getItemId()) {
		  case MENU_ITEM_ABOUT:
			about();
		  default:
			return true;
		}*/

		return super.onOptionsItemSelected(item);
	}

    private void about() {

	}
    
    @Override  
	public boolean onKeyDown(int keyCode,KeyEvent event) {  
	    // back key 
	    if (keyCode == KeyEvent.KEYCODE_BACK) {
	    	startActivity(new Intent(Config.this, MorseSignal.class));
	    	MorseSignal.mCurrentMode = MorseSignal.MODE_MAIN;
	    	return true;
	    } else // if not back key  
	        return super.onKeyDown(keyCode, event);		  
	}
}